//
//  LSHomeCustomFunctionView.m
//  LookScore
//
//  Created by wr on 2017/12/18.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSHomeCustomFunctionView.h"

@implementation LSHomeCustomFunctionView


+(void)addChooseFunctionArray:(NSArray *)FunctionArray selectedArray:(NSArray *)selectedArray view:(UIView *)rootView againChooseBlock:(AgainChooseBlock )againChooseBlock{
    
    
    LSHomeCustomFunctionView *view=[[LSHomeCustomFunctionView alloc]init];
    view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:.5f];
    view.frame=CGRectMake(0, 0, LSScreenWidth, LSScreenHeight);
    view.block=againChooseBlock;
    view.selectedArray=selectedArray.mutableCopy;
    view.FunctionArray=FunctionArray;
    UIWindow *window;// = [[UIApplication sharedApplication].windows lastObject];
    if ( @available(iOS 11.0, *)) {
        window = [UIApplication sharedApplication].keyWindow;
    }else{
        window = [[UIApplication sharedApplication].windows lastObject];
    }
    // 2.添加自己到窗口上
    [window addSubview:view];


    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake((LSScreenWidth-285)/2,(LSScreenHeight-228)/2-30, 285, 228)];
    backView.layer.masksToBounds=YES;
    backView.layer.cornerRadius=5;
    backView.backgroundColor=LSTitleWhiteColor;
    [view addSubview:backView];
   
    UILabel *customLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, backView.width, 50)];
    customLab.font=LSFontSize13;
    customLab.textColor=LSTitleGrayColor1;
    customLab.numberOfLines=2;
    customLab.backgroundColor=LSTitleWhiteColor;
    customLab.textAlignment=NSTextAlignmentCenter;
    customLab.attributedText=[CommonClass setBasicTitle:@"自定义功能" homeTitle:@"" changeTitle:@"\n(限选四个)" color:LSTitleGrayColor3 size:10 isBold:NO];
    [backView addSubview:customLab];
   
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0,50, backView.width, 1)];
    lineView.backgroundColor=LSLineGrayColor;
    [backView addSubview:lineView];
   
    CGFloat width=backView.width/4;
    CGFloat height=(backView.height-50)/2;
    
    for (int i=0; i<FunctionArray.count; i++) {
        NSArray *Array=FunctionArray[i];
        UIButton *gameTypeBu=[UIButton new];
        [gameTypeBu setTitle:Array[1] forState:UIControlStateNormal];
        [gameTypeBu setImage:[UIImage imageNamed:Array[0]] forState:UIControlStateNormal];
        [gameTypeBu setTitleColor:[CommonClass colorWithHexString:@"#4d5665"] forState:UIControlStateNormal];
        gameTypeBu.titleLabel.font=LSFontSize15;
        gameTypeBu.tag=i;
        gameTypeBu.backgroundColor=LSTitleWhiteColor;
        CGFloat widthF=[CommonClass textWidthByString:Array[1] sizeFont:12 layoutWidth:backView.width spaceWidth:0];
        UIImage *image=[UIImage imageNamed:Array[0]];
        gameTypeBu.titleEdgeInsets=UIEdgeInsetsMake(image.size.height,-image.size.width, 0, 0);
        gameTypeBu.imageEdgeInsets=UIEdgeInsetsMake(-20, 0,0, -widthF);
        [gameTypeBu addTarget:view action:@selector(chooseGameTypeEvent:) forControlEvents:UIControlEventTouchUpInside];
        gameTypeBu.frame=CGRectMake(width*(i%4),CGRectGetMaxY(lineView.frame)+height*(i/4), width, height);
        [backView addSubview:gameTypeBu];
        
        
        UIButton *chooseBu=[UIButton new];
        [chooseBu setImage:[UIImage imageNamed:@"HomeUnChoose"] forState:UIControlStateNormal];
        chooseBu.tag=100+i;
        chooseBu.userInteractionEnabled=NO;
        [chooseBu setImage:[UIImage imageNamed:@"HomeChoose"] forState:UIControlStateSelected];
        if ([selectedArray containsObject:Array[1]]) {
            chooseBu.selected=YES;
            gameTypeBu.selected=chooseBu.selected;
        }
        chooseBu.frame=CGRectMake(gameTypeBu.width-15, 5, 10, 10);
        [gameTypeBu addSubview:chooseBu];
       
    }
    

    if (againChooseBlock) {
        againChooseBlock(selectedArray.mutableCopy);
    }
    
}


-(void)chooseGameTypeEvent:(UIButton *)sender{
  
    if (self.selectedArray.count<4) {
        sender.selected=!sender.selected;
        [self.FunctionArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSArray *array=self.FunctionArray[idx];
            if ([array[1] isEqualToString:sender.titleLabel.text]) {
                if (sender.selected) {
                    [self.selectedArray addObject:array[1]];
                }else{
                    [self.selectedArray removeObject:array[1]];
                }
            }
        
        }];
        
    }else{
       
        if (sender.selected) {
            sender.selected=!sender.selected;
            [self.selectedArray removeObject:sender.titleLabel.text];
        }
    }

    for (UIButton *chooseBu in sender.subviews) {
        if (chooseBu.tag==sender.tag+100) {
            chooseBu.selected=sender.selected;
        }
    }
    
    
    if (self.block) {
        self.block(self.selectedArray);
    }
    
    
}
-(void)dismiss{
    [self removeFromSuperview];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
[self dismiss];

    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
